<?php
declare (strict_types = 1);

namespace app\admin\controller;
class ServerSendEvent extends AdminBase
{
    protected function initialize()
    {
        parent::initialize();
    }
    public function notic(){
        $time = date('r');
        $data = [
            'event'=>'message',
            'id'=>'12',
            'retry'=>5000,
            'data'=>"The server time is: {$time}",
        ];
        $res = $this->genData($data);
        return response()->data($res)->contentType('text/event-stream')->cacheControl('no-cache');
        // flush();
    }
    private function genData($data){
        $res = '';
        if(!is_array($data)) return $res;

        foreach ($data as $key => $value) {
            $res = $res.$key.':'.$value."\n";
        }

        return $res."\n";
    }

}
